package com.hibernate;
import com.hibernate.HistoricalData;

import org.hibernate.*; 
import org.hibernate.cfg.Configuration;

import java.io.*;
import java.util.Date;

import org.apache.http.client.*;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import javax.xml.xpath.*;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class YQLDump {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		/*
		 * Grabbed Majority of code for getting XML from Apache Documentation :-|
		 * Implementation is pretty standard. I pass a request. I get a response and every response has an entity - which is the XML file we need :-)
		 * So next I store the content within entity into an InputStrem object - which can handle data at byte level.
		 * After getting the XML into inputstream object I use XPath to traverse the nodes of XML file. In this file I print all the text within quote --> Ask. Tree goes like this.
		 */
		
		//Read config file to get list of symbols.
		
		try 
		{
			// I want to read the config file in one pass. So I append contents with a '+' to string 'config'
			//Then I separate the symbol names from '+' and write it to String array 'symbols'
			//Which is then used to fetch the historical data
			BufferedReader config_reader = new BufferedReader(new FileReader("config.dat"));
		
			String config = new String();
			String line;
			int n_symbols = 0;
			while ((line = config_reader.readLine()) != null)
			{
				n_symbols++;
				config = config.concat(line);
				config = config.concat("+");
			}
			String[] symbols = new String[n_symbols];
			int cursor = 0;
			int curr_symbol = 0;
			while((cursor<=(config.length())-1))
			{
				symbols[curr_symbol++] = config.substring(cursor,config.indexOf("+",cursor));
				cursor = config.indexOf("+",cursor) + 1;
			}
			
			HttpClient httpclient = new DefaultHttpClient();
	        
	        for(int j = 0; j<n_symbols;j++)
	        {
		        HttpGet httpget = new HttpGet("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22"+symbols[j]+"%22)%20and%20startDate=%222010-01-01%22%20and%20endDate=%222011-01-01%22&env=http://datatables.org/alltables.env");
				
				try {
					
					HttpResponse response = httpclient.execute(httpget);
					HttpEntity entity = response.getEntity();
					
					
					InputStream instream = entity.getContent();
					
					/* This block is for debugging
					BufferedReader in = new BufferedReader(new InputStreamReader(instream));
					System.out.println(in.readLine());
					System.out.println(in.readLine());
					System.out.println(in.readLine());
					*/
					
					DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
				    domFactory.setNamespaceAware(true);
				    DocumentBuilder builder = domFactory.newDocumentBuilder();
				    Document doc = builder.parse(instream);			
		
				    XPath xpath = XPathFactory.newInstance().newXPath();
				    XPathExpression expr = xpath.compile("//quote/*/text()");
				    
				    NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
				    System.out.println(nodes.getLength());
			        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
			        Session session =sessionFactory.openSession(); 
			        
				    for (int i = 0; i < nodes.getLength(); i=i+7) {
				    	HistoricalData user1 = new HistoricalData(); 
				        
				    	user1.setsymbol(symbols[j]);		    
				    	user1.setdate((String)nodes.item(i).getNodeValue());
				        user1.setopen(Double.parseDouble(nodes.item(i+1).getNodeValue()));
				        user1.sethigh(Double.parseDouble(nodes.item(i+2).getNodeValue()));
				        user1.setlow(Double.parseDouble(nodes.item(i+3).getNodeValue()));
				        user1.setclose(Double.parseDouble(nodes.item(i+4).getNodeValue()));
				        user1.setvolume(Integer.parseInt(nodes.item(i+5).getNodeValue()));
				    	session.save(user1);
				        session.flush();	
				    }
				    System.out.println("Data written:" + symbols[i]);
				  	    
					        
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (XPathExpressionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
	        }
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

}
